/**
 * Created by Administrator on 2017/8/18.
 */
let mem = require('../util/mem.js');
let log = require('../util/log.js');
let rpc = require('./rpc.js');
let push = require('./push.js');

let action = module.exports;

action.message = function (message, socket, id) {
    let obj = getMessage(message, id);
    if (!obj) {
        return false;
    }
    let now = Date.now();
    if (!socket.requestTime) {
        socket.requestTime = {};
    }
    if (now - socket.requestTime[message] < 1000) {
        console.log('操作太快');
        //push.sendError(10001, id);
        return false;
    }
    if (obj.action != 'globalHeartbeat') {
        log.gate('gate', message);
        socket.updateTime = now;
    }
    socket.requestTime[message] = now;
    let user = null;
    if (mem.clients[id].user) {
        user = mem.clients[id].user;
    }
    
    switch (obj.action) {
        //case 'enterReg':
        //case 'enterLogin':
        //case 'enterAutoReg':
        //case 'enterBluedLogin':
        case 'enterToken':
        case 'enterLogout':
            rpc.send.enter(obj.action, id, null, obj.data);
            break;
        case 'matchStart':
        case 'matchStop':
        case 'matchCustom':
        case 'matchFollow':
        case 'matchList':
        case 'arenaEnter':
        case 'arenaMatchIn':
        case 'arenaMatchOut':
            if (!checkUser(id)) {
                return false;
            }
            rpc.send.match(obj.action, id, user.id, obj.data);
            break;
        case 'gameJoin':
        case 'gameExit':
        case 'gameReady':
        case 'gameAskChip':
        case 'gameAskDZ':
        case 'gameRobDZ':
        case 'gameDouble':
        case 'gameShowCard':
        case 'gameAuto':
        case 'gamePlay':
        case 'gameMsg':
        case 'gamePraise':
            if (!checkUser(id)) {
                return false;
            }
            rpc.send.game(obj.action, id, user.id, obj.data);
            break;
        case 'funcUserInfo':
        case 'funcUserFollow':
        case 'funcUserUnFollow':
        case 'funcRanking':
        case 'funcGoods':
        case 'funcRecharge':
        case 'funcMailList':
        case 'funcMailTake':
        case 'funcMailTakeAll':
        case 'funcGameAmount':
        case 'funcGiveGift':
        case 'funcDailySignIn':
        case 'funcTaskFinish':
        case 'funcOnlineReward':
        case 'funcLoserSupport':
            if (!checkUser(id)) {
                return false;
            }
            rpc.send.func(obj.action, id, user.id, obj.data);
            break;
        case 'activityChipTrade':
        //case 'activityChipBuy':
        //case 'activityChipSell':
        case 'activitySlotMachineJoin':
        case 'activitySlotMachinePlay':
        case 'activitySlotMachineDouble':
        case 'activitySlotMachineDice':
        case 'activitySlotMachineReturn':
        case 'activitySlotMachineExit':
            if (!checkUser(id)) {
                return false;
            }
            rpc.send.activity(obj.action, id, user.id, obj.data);
            break;
        //case 'arenaEnter':
        //    if (!checkUser(id)) {
        //        return false;
        //    }
        //    rpc.send.arena(obj.action, id, user.id, obj.data);
        //    break;
        case 'globalHeartbeat':
            //socket.updateTime = now;
            push.heartbeat(id, now);
            break;
        default :
            if (!checkUser(id)) {
                return false;
            }
            break;
    }
};

action.close = function (socket) {
    if (socket.uuid) {
        if (mem.clients[socket.uuid]) {
            if (mem.clients[socket.uuid].user) {
                rpc.send.match('matchStop', socket.uuid, mem.clients[socket.uuid].user.id);
                rpc.send.game('gameExit', socket.uuid, mem.clients[socket.uuid].user.id);
                rpc.send.activity('activitySlotMachineExit', socket.uuid, mem.clients[socket.uuid].user.id);
                rpc.send.dc('dcUnLink', socket.uuid, mem.clients[socket.uuid].user.id, {
                    'bluedId': mem.clients[socket.uuid].user.bluedId
                });
                log.d('登出', mem.clients[socket.uuid].user.id);
            }
            delete mem.clients[socket.uuid];
        }
    }
    socket.close();
};


function checkUser(id) {
    if (!mem.clients[id].user || !mem.clients[id].user.id) {
        push.sendError(10018, id);
        return false;
    }
    return true;
}

function getMessage(message, id) {
    let err = false;
    let obj = null;
    try {
        obj = JSON.parse(message);
    } catch (e) {
        err = true;
    }
    if (typeof (obj) != 'object') {
        err = true;
    }
    if (err) {
        push.sendError(10003, id);
        return false;
    }
    return obj;
}